package com.ifnk.portaljava.dapWeb.advance;

/**
 * <p>Title:生成菜单XML类 </p>
 *
 * <p>Description:输入用户角色，查寻出可访问的菜单，生成XML返回 </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author  li.guo.hlyt@gmail.com
 * @version 1.0
 * @see com.ifnk.portaljava.dapWeb.advance.Subcription.java
 */

import com.ifnk.portaljava.dapWeb.pojo.MenuRecord;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import java.util.ArrayList;

public class WriteXML {

	public ArrayList targetal = new ArrayList();
/*
	public String writeXML() {

		ArrayList al = new ArrayList();
		al = Subcription.readCDRrecord();
		Element fNode = new Element("HLYT");
		fNode.setAttribute("state", "0");
		fNode.setAttribute("label", "HLYT");

		Document document = new Document(fNode);
		String oldnode = "";
		String oldprotocol = "";
		Element typeNode = null;
		Element protocolNode = null;
		for (int i = 0; i < al.size(); i++) {

			CDRrecord cd = new CDRrecord();
			cd = (CDRrecord) al.get(i);
			if (oldprotocol.equals("")) {
				oldprotocol = cd.getProtocol();
				protocolNode = new Element("node");
				fNode.addContent(protocolNode);
				protocolNode.setAttribute("state", "0");
				protocolNode.setAttribute("label", cd.getProtocol());
			} else {
				if (!oldprotocol.equals(cd.getProtocol())) { 
					oldprotocol = cd.getProtocol();
					protocolNode = new Element("node");
					fNode.addContent(protocolNode);
					protocolNode.setAttribute("state", "0");
					protocolNode.setAttribute("label", cd.getProtocol());
				}
			}

			if (oldnode.equals("")) {
				oldnode = cd.getType();
				typeNode = new Element("node");
				protocolNode.addContent(typeNode);
				typeNode.setAttribute("state", "0");
				typeNode.setAttribute("label", cd.getType());
				Element AttributeNode = new Element("node");
				typeNode.addContent(AttributeNode);
				AttributeNode.setAttribute("state", "0");
				AttributeNode.setAttribute("label", cd.getName());
			} else {
				if (oldnode.equals(cd.getType())) {
					Element AttributeNode = new Element("node");
					typeNode.addContent(AttributeNode);
					AttributeNode.setAttribute("state", "0");
					AttributeNode.setAttribute("label", cd.getDisplayname());
				} else {
					oldnode = cd.getType();
					typeNode = new Element("node");
					protocolNode.addContent(typeNode);
					typeNode.setAttribute("state", "0");
					typeNode.setAttribute("label", cd.getType());
					Element AttributeNode = new Element("node");
					typeNode.addContent(AttributeNode);
					AttributeNode.setAttribute("state", "0");
					AttributeNode.setAttribute("label", cd.getDisplayname());
				}

			}

		}
		XMLOutputter xo = new XMLOutputter();
		String xmlStr = "";
		try {
			xmlStr = xo.outputString(document);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return xmlStr;
	}
*/
	public String writeMenuXml(String role_id) {
		String xmlStr = "";
		Element rootNode = new Element("root");

		Boolean bl = true;// 有儿子

		targetal = Subscription.readMenuRecord(role_id);
		MenuRecord menu = new MenuRecord();
		menu = findnode0_1(targetal);
		if (menu == null) {
			return xmlStr;
		}
		Element fNode = createNode(menu);
		Element lastfNode = null;
		rootNode.addContent(fNode);
		while (targetal.size() > 0) {
			while (bl) {
				MenuRecord bromenu = menu;
				if(bromenu == null){
					break;
				}
				menu = hasChild(menu, targetal);

				if (menu == null) {//没儿子
					menu = hasBrother(bromenu, targetal);
					if(menu==null){//兄弟也没有了
						while(true){
							if(fNode.getParentElement().getAttributeValue("parent") != null){
								menu = getParent(fNode.getParentElement().getAttributeValue("parent"),targetal);
							}else{
								menu = null;
							}
							if(menu != null){
								Element cNode = createNode(menu);
								(fNode.getParentElement()).getParentElement().addContent(cNode);
								fNode = cNode;
								break;
							}else{
								
								fNode = fNode.getParentElement();
								if(targetal.size()==0)break;
							}
						}
						break;
					}else{
						//加上兄弟节点
						Element cNode = createNode(menu);
						fNode.getParentElement().addContent(cNode);
						lastfNode = fNode;
						fNode = cNode ;
					}
				} else {
					Element cNode = createNode(menu);
					fNode.addContent(cNode);
					lastfNode = fNode;
					fNode = cNode;
				}
			}
		}
		XMLOutputter xo = new XMLOutputter();
		try {
			xmlStr = xo.outputString(rootNode.getChildren("menuitem"));
//			System.out.println(xmlStr);

		} catch (Exception e) {
			e.printStackTrace();
		}
		//		System.out.println("xmlStr:"+xmlStr);
		return xmlStr;
	}

	/* 查找0_1节点 */
	public MenuRecord findnode0_1(ArrayList al) {
		for (int i = 0; i < al.size(); i++) {
			MenuRecord mn = new MenuRecord();
			mn = (MenuRecord) al.get(i);
//			System.out.println("mn.getParent:"+mn.getParent()+":mn.getNo():"+mn.getNo());
			if ((mn.getParent() == 0) ) {// 找到第一个节点
				al.remove(i);
				targetal = al;
				return mn;
			}
		}
		return null;
	}

	/* 生成节点 */
	public Element createNode(MenuRecord menu) {
		Element fNode = new Element("menuitem");
		fNode.setAttribute("label", menu.getMenu_name());
		fNode.setAttribute("name", menu.getMenu_name());
		try{
			fNode.setAttribute("data", menu.getLocation());
		}catch(Exception e){
			fNode.setAttribute("data", "");
		}
		fNode.setAttribute("parent", String.valueOf(menu.getParent()));
		return fNode;
	}

	/* 是否有儿子节点 */
	public MenuRecord hasChild(MenuRecord parentMenu, ArrayList al) {
		int parentID = parentMenu.getId();
		for (int i = 0; i < al.size(); i++) {
			MenuRecord mn = new MenuRecord();
			mn = (MenuRecord) al.get(i);
			if (mn.getParent() == parentID) {//
				al.remove(i);
				targetal = al;
				return mn;
			}
		}
		return null;
	}

	public MenuRecord hasBrother(MenuRecord brotherMenu, ArrayList al) {
		int parentID = brotherMenu.getParent();
		for (int i = 0; i < al.size(); i++) {
			MenuRecord mn = new MenuRecord();
			mn = (MenuRecord) al.get(i);
			if (mn.getParent() == parentID) {//
				al.remove(i);
				targetal = al;
				return mn;
			}
		}
		return null;
	}

	public MenuRecord getParent(String parentId, ArrayList al) {
		int parentID = Integer.valueOf(parentId);
		for (int i = 0; i < al.size(); i++) {
			MenuRecord mn = new MenuRecord();
			mn = (MenuRecord) al.get(i);
			if (mn.getParent() == parentID) {//
				al.remove(i);
				targetal = al;
				return mn;
			}
		}
		return null;
	}

	public static void main(String arg[]) {
		try {
			//			File f1 = new File("d:/mystruts.xml");
			//			// XMLOutputter xo=new XMLOutputter(" ",true,"GB2312");
			//			XMLOutputter xo = new XMLOutputter();
			//			FileWriter fw = new FileWriter(f1);
			//			WriteXML wx = new WriteXML();
			//			String Str = wx.writeMenuXml("6,7");
			//			fw.close();
			//WriteXML w = new WriteXML();
			//String xml = w.writeMenuXml("28,");
//			System.out.println("xml:"+xml);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
/**
 * <root> <menuitem label="MenuItem A" > <menuitem label="SubMenuItem A-1"
 * enabled="false"/> <menuitem label="SubMenuItem A-2"/> </menuitem> <menuitem
 * label="MenuItem B" type="check" toggled="true"/> <menuitem label="MenuItem C"
 * type="check" toggled="false"/> <menuitem type="separator"/> <menuitem
 * label="MenuItem D" > <menuitem label="SubMenuItem D-1" type="radio"
 * groupName="one"/> <menuitem label="SubMenuItem D-2" type="radio"
 * groupName="one" toggled="true"/> <menuitem label="SubMenuItem D-3"
 * type="radio" groupName="one"/> </menuitem> </root>
 */
